//
//  XLNetwork.swift
//  微博
//
//  Created by macbook on 16/9/29.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

import AFNetworking


enum MethRype:String {
    case GET = "GET"
    case POST = "POST"
}

class XLNetwork: AFHTTPSessionManager {
    
    //设置全局访问点
    static let sharedTools:XLNetwork = {
        
        let tools = XLNetwork()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
        
    }()
    
    func request(methop:MethRype,urlString:String,parameters:AnyObject,success:(reponse:AnyObject?)->(),faileurl:(error:NSError)->()){
        
        //GET
        if methop == .GET {
            
            self.GET(urlString, parameters: parameters, progress: nil, success: { (_, res) in
                
                success(reponse: res)
                }, failure: { (_, error) in
                    
                    faileurl(error: error)
            })
        }else{
            self.POST(urlString, parameters: parameters, progress: nil, success: { (_, res) in
                success(reponse: res)
                }, failure: { (_, error) in
                    faileurl(error: error)
            })
            
        }
        
    }
}

extension XLNetwork{

    //请求token
    func oautnLoadUserAccout(code:String,success:(response:AnyObject?)->(),faileuer:(error:NSError)->()){
    
        //请求的url
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = [
            
            "client_id":"1387814882",
            "client_secret":"0361e14f9de9c64b0f5f0c025472186f",
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":"http://www.baidu.com"
        
        ]
        
        self.request(.POST, urlString: urlString, parameters: parameters, success: success, faileurl: faileuer)
        
    }
    
    //获取个人设置
    func oauthUserInfo(userAccont:XLUserAccount,success:(responst:AnyObject?)->(),faileuer:(error:NSError)->()){
    
        //请求的url
        let url = "https://api.weibo.com/2/users/show.json"
        //请求的参数
        let parmart = [
        
            "access_token":userAccont.access_token!,
            "uid":userAccont.uid!
        ]
        
        self.request(.GET, urlString: url, parameters: parmart, success: success, faileurl: faileuer)
     
    }
    
    
    
}















